"""crawlSpider  用于爬取有规律的网站"""
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule


class CrawlhospitalSpider(CrawlSpider):
    name = 'crawlHospital'
    allowed_domains = ['yyk.99.com.cn']
    start_urls = ['https://yyk.99.com.cn/tianjin']

    rules = (
        Rule(LinkExtractor(allow=r'https://yyk.99.com.cn/[a-z]*/\d+/$'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        item = {}
        print("爬取的链接是:>>>>>>>>", response.url)
        div1 = response.xpath(".//div[@class='wrapper']//div[@class='wrap-mn']")
        item["c_hospital_name"] = div1.xpath(".//h1/text()").extract_first()  # 医院名称
        item["level"] = div1.xpath("./div/span[1]/text()").extract_first()  # 医院等级
        property = div1.xpath("./dl[@class='wrap-info']//p[2]/text()").extract_first()  # 性质
        proList = property.split("/")
        item["n_ownership_type"] = proList[0]  # 公立还是私立
        item["n_type"] = proList[1]  # 医院类型，综合/专科/其他
        item["c_phone"] = div1.xpath("./dl[@class='wrap-info']//p[3]/em/text()").extract_first()  # 电话
        item["c_address"] = div1.xpath("./dl[@class='wrap-info']//p[4]/em/text()").extract_first()  # 地址

        item["c_dean"] = response.xpath(
            ".//table[@class='present-table']/tr[2]/td[2]/span/text()").extract_first()  # 院长
        item["createYear"] = response.xpath(
            ".//table[@class='present-table']/tr[2]/td[4]/span/text()").extract_first()  # 建院年份

        item["yibao"] = response.xpath(
            ".//table[@class='present-table']/tr[4]/td[6]/span/text()").extract_first()  # 是否医保
        description1 = response.xpath(
            "normalize-space(.//div[@class='present-wrap1']/div/div/text())").extract_first()  # 没有p标签的简介
        description2 = response.xpath(
            "normalize-space(.//div[@class='present-wrap1']/div/div/p/text())").extract_first()  # 带p标签的简介
        item["c_introduction"] = description1
        # 如果p标签中没有数据  就取div里的数据
        if description1 == "":
            item["c_introduction"] = description2
        if item["c_introduction"] == "" or item["c_introduction"] is None:
            item["c_introduction"] = "暂无医院简介"
        div3 = response.xpath(".//div[@class='crumb']")
        item["c_province"] = div3.xpath("./p/a[2]/text()").extract_first()  # 省
        item["c_city"] = div3.xpath("./p/a[3]/text()").extract_first()  # 市
        item["c_area"] = div3.xpath("./p/a[4]/text()").extract_first()  # 区
        return item

#
